#include "aky_program/aky_com_include.h"

static void my_event_cb(lv_obj_t * obj, lv_event_t event);
static void switch_show_stat(uint8_t stat);
void engine_stat_pro(void);
void mem_4g_table_init(void);
void clr_pres_val(void);

static lv_obj_t *label_version;          //版本日期
static lv_obj_t *label_title;          //标题
static lv_obj_t *label_content;          //内容
static lv_obj_t *label_cal;          //校正
static lv_obj_t *label_linear;          //线性
static lv_obj_t *label_write_mem;          //写记忆
static lv_obj_t *label_confirm;          //确认
static lv_obj_t *label_back;          //返回
static lv_obj_t *label_mount_sd;          //挂载SD卡
static lv_obj_t *label_mount_flash;          //挂载flash
static lv_obj_t *label_clr_user;          //清除用户

static lv_obj_t *label_emc;          //emc模式
static lv_obj_t *label_life_test;          //寿命模式
static lv_obj_t *label_fast_test;          //快速测试
static lv_obj_t *label_adjust_train;          //可调手动训练
static lv_obj_t *label_4g_test;          //4g测试


static uint32_t u32_tick_last = 0;
char fmt_buf_final[512];
static s_mem_t s_mem_4g_table[10];
static uint16_t up_load_index = 0;
static uint16_t retry_cnt = 0;
static uint16_t time_out = 0;

typedef enum
{
    ENGINE_STAT_IDLE = 0,
    ENGINE_STAT_CAL,
    ENGINE_STAT_LINEAR,
    ENGINE_STAT_CLR_USER,
    ENGINE_STAT_WRITE_FUL_MEM,
    ENGINE_STAT_MOUNT_FLASH,
    ENGINE_STAT_MOUNT_SD,
    ENGINE_STAT_4G_TEST,
    ENGINE_STAT_LIFE_TEST,
    ENGINE_STAT_EMC,
    ENGINE_STAT_FAST_TEST,

    ENGINE_STAT_UNDEF,
}e_engine_stat_t;

e_engine_stat_t e_engine_stat = ENGINE_STAT_IDLE;

static uint16_t step = 0;
extern stru_aky_task_0_t s_aky_task_0;
extern stru_aky_task_1_t s_aky_task_1;
/*
 * 界面回调函数
 */
static void screen012_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    engine_stat_pro();
    all_power_control_pro();
    off_time_clr();
}

static void screen012_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    e_engine_stat = ENGINE_STAT_IDLE;
    g_flag_emc_mode = 0;
            

    label_version = synwit_ui_find_lv_obj(WIDGET_LABEL27);
    label_title = synwit_ui_find_lv_obj(WIDGET_LABEL25);
    label_content = synwit_ui_find_lv_obj(WIDGET_LABEL26);
    label_cal  = synwit_ui_find_lv_obj(WIDGET_LABEL46);
    label_linear = synwit_ui_find_lv_obj(WIDGET_LABEL47);
    label_write_mem = synwit_ui_find_lv_obj(WIDGET_LABEL48);
    label_confirm = synwit_ui_find_lv_obj(WIDGET_LABEL45);
    label_back = synwit_ui_find_lv_obj(WIDGET_LABEL49);
    label_mount_sd = synwit_ui_find_lv_obj(WIDGET_LABEL50);
    label_mount_flash = synwit_ui_find_lv_obj(WIDGET_LABEL51);
    label_clr_user = synwit_ui_find_lv_obj(WIDGET_LABEL52);
    label_emc = synwit_ui_find_lv_obj(WIDGET_LABEL54);
    label_life_test = synwit_ui_find_lv_obj(WIDGET_LABEL55);
    label_fast_test = synwit_ui_find_lv_obj(WIDGET_LABEL56);
    label_adjust_train = synwit_ui_find_lv_obj(WIDGET_LABEL57);
    label_4g_test = synwit_ui_find_lv_obj(WIDGET_LABEL58);

    lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_40.hmf", false);
    lv_obj_set_style_local_text_font(label_title,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);
    lv_obj_set_style_local_text_font(label_content,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);


    lv_obj_set_click(label_confirm,true);
    lv_obj_set_click(label_back,true);
    lv_obj_set_click(label_cal,true);
    lv_obj_set_click(label_linear,true);
    lv_obj_set_click(label_write_mem,true);
    lv_obj_set_click(label_mount_sd,true);
    lv_obj_set_click(label_mount_flash,true);
    lv_obj_set_click(label_clr_user,true);
    lv_obj_set_click(label_emc,true);
    lv_obj_set_click(label_life_test,true);
    lv_obj_set_click(label_fast_test,true);
    lv_obj_set_click(label_adjust_train,true);
    lv_obj_set_click(label_4g_test,true);

    switch_show_stat(0);
    static uint8_t flag_init_onece = 0;
    if (flag_init_onece == 0)
    {
        flag_init_onece = 1;
        lv_label_set_text_fmt(label_version,"ver:%d/%d",vermon,verday);
        lv_obj_set_width(label_version,400);
        /* code */
    }
    

    lv_obj_set_event_cb(label_confirm, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_cal, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_linear, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_write_mem, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_mount_sd, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_mount_flash, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_clr_user, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_4g_test, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_adjust_train, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_life_test, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_emc, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_fast_test, my_event_cb);   /*Assign an event callback*/

    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen012_stop(synwit_id_t screen_id)
{

    /* 准备切换到其它界面前，这个接口会被调用。 */
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);
    aky_block_delay_ms(100);
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);
    aky_block_delay_ms(100);
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);

}

static int screen012_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen012_cb_obj = {
    .favorite_stop_policy = screen012_favorite_stop_policy,
    .on_screen_start = screen012_start,
    .on_screen_stop = screen012_stop,
    .on_screen_timer = screen012_on_timer,
};
static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (e_engine_stat == ENGINE_STAT_IDLE)
    {
        if (obj == label_adjust_train)
        {
            if (event == LV_EVENT_CLICKED)
            {
                synwit_ui_load_screen(SCREEN014);
            }
        }
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (g_flag_jump_to_fast_test == 1)
                {
                    g_flag_jump_to_fast_test = 0;
                    synwit_ui_load_screen(SCREEN002);
                }
                else
                {
                    synwit_ui_load_screen(SCREEN005);
                }    
            }
        }
        if (obj == label_cal)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_CAL;
            }
        }
        if (obj == label_linear)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_LINEAR;
            }
        }       
        if (obj == label_clr_user)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_CLR_USER;
            }
        }
        if (obj == label_write_mem)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_WRITE_FUL_MEM;
            }
        }
        if (obj == label_mount_flash)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_MOUNT_FLASH;
            }
        }        
        if (obj == label_mount_sd)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_MOUNT_SD;
            }
        }        
        if (obj == label_4g_test)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_4G_TEST;
            }
        }        
        if (obj == label_life_test)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_LIFE_TEST;
            }
        }        
        if (obj == label_fast_test)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_FAST_TEST;
            }
        }        
        if (obj == label_emc)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_EMC;
            }
        }        
        return;
    }
    else if (e_engine_stat == ENGINE_STAT_LINEAR)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
                aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
                aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);

            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_CLR_USER)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_4G_TEST)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_LIFE_TEST)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
                aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
                aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_FAST_TEST)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;

                voice_stop();
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_EMC)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_MOUNT_FLASH)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_MOUNT_SD)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_WRITE_FUL_MEM)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
            }
        }
    }
    else if (e_engine_stat == ENGINE_STAT_CAL)
    {
        if (obj == label_confirm)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (step == 2)
                {
                    aky_task_0_send_cmd(TASK0_BP_CALOK_Command);
                    aky_task_1_send_cmd(TASK1_BP_CALOK_Command);
                    u32_tick_last = g_u32_tick_cnt;

                    step = 3;
                }
            }
        }
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_engine_stat = ENGINE_STAT_IDLE;
                aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
                aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);

            }
        }
    }
}

static void switch_show_stat(uint8_t stat)
{
    lv_obj_set_hidden(label_content,true);
    lv_obj_set_hidden(label_confirm,true);

    lv_obj_set_hidden(label_cal,true);
    lv_obj_set_hidden(label_linear,true);
    lv_obj_set_hidden(label_write_mem,true);
    lv_obj_set_hidden(label_mount_sd,true);
    lv_obj_set_hidden(label_mount_flash,true);
    lv_obj_set_hidden(label_clr_user,true);
    lv_obj_set_hidden(label_emc,true);
    lv_obj_set_hidden(label_life_test,true);
    lv_obj_set_hidden(label_fast_test,true);
    lv_obj_set_hidden(label_adjust_train,true);
    lv_obj_set_hidden(label_4g_test,true);

    if (stat == 0)
    {
        lv_obj_set_hidden(label_back,false);

        lv_obj_set_hidden(label_cal,false);
        lv_obj_set_hidden(label_linear,false);
        lv_obj_set_hidden(label_write_mem,false);
        lv_obj_set_hidden(label_mount_sd,false);
        lv_obj_set_hidden(label_mount_flash,false);
        lv_obj_set_hidden(label_clr_user,false);

        lv_obj_set_hidden(label_emc,false);
        lv_obj_set_hidden(label_life_test,false);
        lv_obj_set_hidden(label_fast_test,false);
        lv_obj_set_hidden(label_adjust_train,false);
        lv_obj_set_hidden(label_4g_test,false);

        lv_label_set_text_fmt(label_title,"工程模式");

    }
    else if (stat == 1)
    {
        lv_obj_set_hidden(label_content,false);
        lv_obj_set_hidden(label_confirm,false);

        lv_label_set_text_fmt(label_title,"校压模式");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 2)
    {
        //清除用户
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"清除用户");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 3)
    {
        //写满记忆
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"写满记忆");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 4)
    {
        //写满记忆
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"挂载flash");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 5)
    {
        //写满记忆
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"挂载SD");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 6)
    {
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"线性模式");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 7)
    {
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"4g模块测试");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 8)
    {
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"寿命模式");
        lv_label_set_text_fmt(label_content,"");

    }
    else if (stat == 9)
    {
        lv_obj_set_hidden(label_content,false);

        lv_label_set_text_fmt(label_title,"快速测试模式");
        lv_label_set_text_fmt(label_content,"");

    }
}
volatile uint8_t g_flag_emc_mode = 0;
void engine_stat_pro(void)
{
    static e_engine_stat_t e_engine_stat_last = ENGINE_STAT_UNDEF;
    static uint16_t time_cnt = 0;
    static uint16_t send_cnt = 0;

    if (e_engine_stat_last != e_engine_stat)
    {
        e_engine_stat_last = e_engine_stat;
        step = 0;
        send_cnt = 0;
        u32_tick_last = g_u32_tick_cnt;
    }
    s_aky_task_0 = aky_task_0_get_data();
    s_aky_task_1 = aky_task_1_get_data();


    switch (e_engine_stat)
    {
    case ENGINE_STAT_IDLE:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(0);
            time_cnt = 0;

        }
        else if (step == 1)
        {
            /* code */
        }

        break;
    }
    case ENGINE_STAT_WRITE_FUL_MEM:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(3);
            time_cnt = 0;
            lv_label_set_text_fmt(label_content,"写入记忆中...",MAX_MEM_SIZE);
        }
        else if (step == 1)
        {
            time_cnt++;
            if (time_cnt > 100 / 10)
            {
                time_cnt = 0;
                step = 2;
            }
            
        }
        else if (step == 2)
        {
            step = 3;
            write_full_mem_bank(g_e_user);
            lv_label_set_text_fmt(label_content,"已写入%d组记忆",MAX_MEM_SIZE);
        }

        break;
    }
    case ENGINE_STAT_CLR_USER:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(2);
            time_cnt = 0;
            clr_mem_bank_complete(USER_1);
            clr_mem_bank_complete(USER_2);
            clr_mem_bank_complete(USER_3);
            lv_label_set_text_fmt(label_content,"已清除所有用户");
        }
        else if (step == 1)
        {
        }
        break;
    }
    case ENGINE_STAT_MOUNT_SD:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(5);
            time_cnt = 0;
            lv_label_set_text_fmt(label_content,"已挂载SD,连接typec到pc查看");

        }
        else if (step == 1)
        {
            time_cnt++;
            if (time_cnt > 100 / 10)
            {
                time_cnt = 0;
                step = 2;
                mount_sd_to_usb();
            }
        }
        break;
    }
    case ENGINE_STAT_MOUNT_FLASH:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(4);
            time_cnt = 0;
            lv_label_set_text_fmt(label_content,"已挂载flash,连接typec到pc查看");

        }
        else if (step == 1)
        {
            time_cnt++;
            if (time_cnt > 100 / 10)
            {
                time_cnt = 0;
                step = 2;
                mount_spi_flash_to_usb();
            }
        }
        break;
    }
    case ENGINE_STAT_LINEAR:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(6);
            lv_label_set_text_fmt(label_title,"线性模式");

            time_cnt = 0;
            clr_pres_val();
            Task0_BP.version_mon = 0;
            Task0_BP.version_date = 0;
            Task0_BP.flag_get_engi_pres_cmd = 0;
            time_cnt = 0;
            send_cnt = 0;
        }
        else if (step == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_C_Command);
                aky_task_1_send_cmd(TASK1_C_Command);
                send_cnt++;
                if (send_cnt >= 20)
                {
                    printf("send_cnt out\n");
                    send_cnt = 0;
                    step = 2;
                }
                if (Task0_BP.version_mon || Task0_BP.version_date)
                {
                    printf("get ver suc send_cnt = %d\n",send_cnt);
                    send_cnt = 0;
                    step = 2;
                }
                
            }
        }
        else if (step == 2)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                lv_label_set_text_fmt(label_content,"正在校零");

                if (Task0_BP.flag_get_engi_pres_cmd == 1)
                {
                    printf("get flag_get_engi_pres_cmd suc\n");
                    step = 3;
                }
                time_cnt++;
                if (time_cnt > 20)
                {
                    printf("time_cnt out\n");

                   time_cnt = 0;
                    step = 3;
                }
                
            }
        }
        else if (step == 3)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                lv_label_set_text_fmt(label_content,"rx0左侧实时压力= %d.%d mmgh rx1右侧实时压力= %d.%d mmgh",s_aky_task_0.pres_mmg / 10,s_aky_task_0.pres_mmg % 10 ,s_aky_task_1.pres_mmg / 10,s_aky_task_1.pres_mmg % 10);
                lv_label_set_text_fmt(label_version,"ver:%d/%d  rx0left ver:%d/%d\nrx1right ver:%d/%d",vermon,verday,s_aky_task_0.version_mon,s_aky_task_0.version_date,s_aky_task_1.version_mon,s_aky_task_1.version_date);
            }
        }        
        break;
    }   
    case ENGINE_STAT_4G_TEST:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(7);
            sprintf(fmt_buf,"\nIMEI = %s",IMEI);

            time_cnt = 0;
            up_load_index = 0;
            mem_4g_table_init();
            time_out = 0;
            #if 00
            up_load_index = 9;
            #endif

        }
        else if (step == 1)
        {

            g_s_mem_send = s_mem_4g_table[up_load_index];
            time_out = 0;

            aky_task_2_send_cmd(TASK2_SEND_MEM);

            // if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                sprintf(fmt_buf2,"\n 正在发送第%d组",up_load_index);

                sprintf(fmt_buf_final," ");

                strcat(fmt_buf_final,fmt_buf);
                strcat(fmt_buf_final,fmt_buf2);
                lv_label_set_text_fmt(label_content,"%s",fmt_buf_final);
            }
            step = 2;
        }
        else if (step == 2)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                if (s_task_2.e_aky_task_2_stat == TASK_2_get_succes)
                {
                    up_load_index++;
                    if(up_load_index >= 10)
                    {
                        up_load_index = 0;
                        step = 3;
                    }
                    else
                    {
                        step = 1;
                    }
                }
                else if (s_task_2.e_aky_task_2_stat == TASK_2_get_fail)
                {
                    //重发
                    g_s_mem_send = s_mem_4g_table[up_load_index];
                    aky_task_2_send_cmd(TASK2_SEND_RESULT);
                    printf("retry\n");
    
                    retry_cnt++;
                    if (retry_cnt > 3)
                    {
                        retry_cnt = 0;
                        step = 20;
                        printf("retry_cnt > 3 SYN_END\n");
                    }
                    
                }
                else
                {
                    //超时
                    time_out++;
                    if (time_out > 30000 / 300)
                    {
                        time_out = 0;
                        step = 20;
                        printf("time_out  SYN_END\n");
                    }
                }
            }
        }
        else if (step == 3)
        {
            step++;
            sprintf(fmt_buf2,"\n 发送成功");

            sprintf(fmt_buf_final," ");

            strcat(fmt_buf_final,fmt_buf);
            strcat(fmt_buf_final,fmt_buf2);
            lv_label_set_text_fmt(label_content,"%s",fmt_buf_final);

        }
        else if (step == 20)
        {
            step++;
            sprintf(fmt_buf2,"\n 发送失败");

            sprintf(fmt_buf_final," ");

            strcat(fmt_buf_final,fmt_buf);
            strcat(fmt_buf_final,fmt_buf2);
            lv_label_set_text_fmt(label_content,"%s",fmt_buf_final);

        }

        break;
    }   
    case ENGINE_STAT_LIFE_TEST:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(8);
            lv_label_set_text_fmt(label_title,"寿命测试模式");

            time_cnt = 0;
            clr_pres_val();
            send_cnt = 0;
        }
        else if (step == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_LifeTest_Command);
                aky_task_1_send_cmd(TASK1_LifeTest_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step = 2;
                }
            }
        }
        
        else if (step == 2)
        {
            if (g_u32_tick_cnt - u32_tick_last > 1000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                lv_label_set_text_fmt(label_content,"rx0左侧寿命次数= %d  rx1右侧寿命次数= %d ",s_aky_task_0.Lefttestnumber,s_aky_task_1.Lefttestnumber);
            }
        }


        break;
    }   
    case ENGINE_STAT_FAST_TEST:
    {
        if (step == 0)
        {
            step++;
            // switch_show_stat(9);
            // lv_label_set_text_fmt(label_title,"快速测试模式");

            time_cnt = 0;
            clr_pres_val();
            synwit_ui_load_screen(SCREEN015);

            // send_cnt = 0;
            // mem_4g_table_init();
            // lv_label_set_text_fmt(label_content,"left sys = %d dia = %d pulse = %d \nright sys = %d dia = %d pulse = %d ",s_mem_4g_table[0].s_left_bp.sys,s_mem_4g_table[0].s_left_bp.dia,s_mem_4g_table[0].s_left_bp.pul,s_mem_4g_table[0].s_right_bp.sys,s_mem_4g_table[0].s_right_bp.dia,s_mem_4g_table[0].s_right_bp.pul);
        }
        // else if (step == 1)
        // {
        //     step++;
        //     Queue_Clear(&qVoice);
        //     {
        //         Queue_Push(&qVoice, PREADAP_END); 
        //     }
        //     inner_voice_combine_cmd(INNER_VOICE_PRES_UNIT_MMG, s_mem_4g_table[0].s_left_bp.sys, s_mem_4g_table[0].s_left_bp.dia,s_mem_4g_table[0].s_right_bp.sys,s_mem_4g_table[0].s_right_bp.dia, s_mem_4g_table[0].s_left_bp.pul);
        //     uint8_t who_level = get_bar_level_by_sys_dia(s_mem_4g_table[0].s_left_bp.sys,s_mem_4g_table[0].s_left_bp.dia);


        //     inner_voice_who(who_level);

        //     {
        //         Queue_Push(&qVoice, DATA_SENDING); 
        //     }


        //     voice_play_array();
        //     u32_tick_last = g_u32_tick_cnt;


        // }
        // else if (step == 2)
        // {
        //     if (get_voice_busy_stat() == 1)
        //     {
        //         u32_tick_last = g_u32_tick_cnt;
        //     }

        //     if (g_u32_tick_cnt - u32_tick_last > 3000 / 10)
        //     {
        //         step = 1;
        //     }
        // }
        break;
    }   
    case ENGINE_STAT_EMC:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(8);
            lv_label_set_text_fmt(label_title,"EMC测试模式");

            time_cnt = 0;
            clr_pres_val();
            send_cnt = 0;
        }
        else if (step == 1)
        {
            g_flag_emc_mode = 1;
            //去测试
            synwit_ui_load_screen(SCREEN008);
        }
        
        break;
    }   

    case ENGINE_STAT_CAL:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(1);
            lv_obj_set_hidden(label_back,true);

            time_cnt = 0;
            clr_pres_val();
            send_cnt = 0;
        }
        else if (step == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_CAL_Command);
                aky_task_1_send_cmd(TASK1_CAL_Command);
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step = 2;
                }
            }
        }

        if (step == 2)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                lv_label_set_text_fmt(label_content,"rx0左侧实时压力= %d.%d mmgh rx1右侧实时压力= %d.%d mmgh",s_aky_task_0.pres_mmg / 10,s_aky_task_0.pres_mmg % 10 ,s_aky_task_1.pres_mmg / 10,s_aky_task_1.pres_mmg % 10);
            }
        }
        else if (step == 3)
        {
            step = 4;
            lv_label_set_text_fmt(label_content,"校正压力中");

        }
        else if (step == 4)
        {
            if (g_u32_tick_cnt - u32_tick_last > 2000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                step = 5;
                lv_obj_set_hidden(label_confirm,true);
                sprintf(fmt_buf,"");
                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_CAL_SUCC)
                {
                    sprintf(fmt_buf2,"\n左侧校正成功");

                }
                else
                {
                    sprintf(fmt_buf2,"\n左侧校正失败");
                }
                strcat(fmt_buf, fmt_buf2);

                if (s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_CAL_SUCC)
                {
                    sprintf(fmt_buf2,"\n右侧校正成功");

                }
                else
                {
                    sprintf(fmt_buf2,"\n右侧校正失败");
                }
                strcat(fmt_buf, fmt_buf2);
                lv_label_set_text_fmt(label_content,fmt_buf);

                step = 5;
                lv_obj_set_hidden(label_back,false);

            }

            // if (g_u32_tick_cnt - u32_tick_last > 1000 / 10)
            // {
            //     u32_tick_last = g_u32_tick_cnt;
            //     lv_label_set_text_fmt(label_content,"rx0左侧实时压力= %d.%d mmgh rx1右侧实时压力= %d.%d mmgh",s_aky_task_0.pres_mmg / 10,s_aky_task_0.pres_mmg % 10 ,s_aky_task_1.pres_mmg / 10,s_aky_task_1.pres_mmg % 10);
            // }
        }
        break;
    }

    default:
        break;
    }
}


void mem_4g_table_init(void)
{
    for (uint16_t i = 0; i < 10; i++)
    {
        s_mem_4g_table[i].s_time = g_s_time;
        if (s_mem_4g_table[i].s_time.month != 1)
        {
            s_mem_4g_table[i].s_time.month -= 1;
        }

        if (s_mem_4g_table[i].s_time.day != 1)
        {
            s_mem_4g_table[i].s_time.day -= 1;
        }

        s_mem_4g_table[i].s_time.hour += i;
        if (s_mem_4g_table[i].s_time.hour > 23)
        {
            s_mem_4g_table[i].s_time.hour = 0;
        }

        s_mem_4g_table[i].s_time.min += i;
        if (s_mem_4g_table[i].s_time.min > 59)
        {
            s_mem_4g_table[i].s_time.min = 0;
        }
        
        s_mem_4g_table[i].s_left_bp.sys = 100 + i;
        s_mem_4g_table[i].s_left_bp.dia = 111;
        s_mem_4g_table[i].s_left_bp.pul = 66;

        s_mem_4g_table[i].s_right_bp.sys = 200 + i;
        s_mem_4g_table[i].s_right_bp.dia = 133;
        s_mem_4g_table[i].s_right_bp.pul = 111;

        s_mem_4g_table[i].s_left_bp_start.sys = 111;
        s_mem_4g_table[i].s_left_bp_start.dia = 100+i;
        s_mem_4g_table[i].s_left_bp_start.pul = 66;

        s_mem_4g_table[i].s_right_bp_start.sys = 222;
        s_mem_4g_table[i].s_right_bp_start.dia = 200+i;
        s_mem_4g_table[i].s_right_bp_start.pul = 111;

    }

    s_mem_4g_table[0].s_time = g_s_time;
    s_mem_4g_table[0].flag_ful_dat = 1;
    s_mem_4g_table[0].s_train_time.min = 50;
    s_mem_4g_table[0].s_train_time.secon = 0;
    s_mem_4g_table[0].TrainMode = 5;
    s_mem_4g_table[0].TrainPressure = s_mem_4g_table[0].s_left_bp.sys + 50;


    s_mem_4g_table[1].flag_ful_dat = 1;
    s_mem_4g_table[1].s_train_time.min = 32;
    s_mem_4g_table[1].s_train_time.secon = 0;
    s_mem_4g_table[1].TrainMode = 1;
    s_mem_4g_table[1].TrainPressure = 180;

    s_mem_4g_table[2].flag_ful_dat = 1;
    s_mem_4g_table[2].s_train_time.min = 50;
    s_mem_4g_table[2].s_train_time.secon = 0;
    s_mem_4g_table[2].TrainMode = 2;
    s_mem_4g_table[2].TrainPressure = 180;

    s_mem_4g_table[3].flag_ful_dat = 1;
    s_mem_4g_table[3].s_train_time.min = 50;
    s_mem_4g_table[3].s_train_time.secon = 0;
    s_mem_4g_table[3].TrainMode = 3;
    s_mem_4g_table[3].TrainPressure = 200;

    s_mem_4g_table[4].flag_ful_dat = 1;
    s_mem_4g_table[4].s_train_time.min = 50;
    s_mem_4g_table[4].s_train_time.secon = 0;
    s_mem_4g_table[4].TrainMode = 4;
    s_mem_4g_table[4].TrainPressure = 220;

    s_mem_4g_table[5].flag_ful_dat = 0;
    s_mem_4g_table[5].s_train_time.min = 25;
    s_mem_4g_table[5].s_train_time.secon = 25;
    s_mem_4g_table[5].TrainMode = 4;
    s_mem_4g_table[5].TrainPressure = 220;

    s_mem_4g_table[5].flag_ful_dat = 0;
    s_mem_4g_table[6].s_train_time.min = 25;
    s_mem_4g_table[6].s_train_time.secon = 25;
    s_mem_4g_table[6].TrainMode = 3;
    s_mem_4g_table[6].TrainPressure = 200;


    s_mem_4g_table[5].flag_ful_dat = 0;
    s_mem_4g_table[7].s_train_time.min = 25;
    s_mem_4g_table[7].s_train_time.secon = 25;
    s_mem_4g_table[7].TrainMode = 2;
    s_mem_4g_table[7].TrainPressure = 180;

    s_mem_4g_table[5].flag_ful_dat = 0;
    s_mem_4g_table[8].s_train_time.min = 16;
    s_mem_4g_table[8].s_train_time.secon = 16;
    s_mem_4g_table[8].TrainMode = 1;
    s_mem_4g_table[8].TrainPressure = 180;

    s_mem_4g_table[5].flag_ful_dat = 0;
    s_mem_4g_table[9].s_train_time.min = 23;
    s_mem_4g_table[9].s_train_time.secon = 25;
    s_mem_4g_table[9].TrainMode = 5;
    s_mem_4g_table[9].TrainPressure = s_mem_4g_table[9].s_left_bp.sys + 50;
}

void clr_pres_val(void)
{
    Task0_BP.pres_mmg = 0;
    Task1_BP.pres_mmg = 0;
}
